package com.gec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gec.common.CustomException;
import com.gec.dto.DishDto;
import com.gec.dto.SetmealDto;
import com.gec.entity.*;
import com.gec.mapper.SetmealMapper;
import com.gec.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * </p>
 *
 * @author : [chenxys]
 * @version : [v1.0]
 * @createTime : [2022/7/5 17:28]
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;


    @Override
    public IPage<SetmealDto> selectByCondition(IPage<Setmeal> page, String params) {
        // 最后的封装结果
        Page<SetmealDto> dtoPage = new Page<>();
        // 构造条件
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        // 模糊查询
        queryWrapper.like(!ObjectUtils.isEmpty(params),Setmeal::getName,params);
        // 排序条件
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);
        // 查询
        page = this.page(page,queryWrapper);
        // 拷贝对象
        BeanUtils.copyProperties(page,dtoPage,"records");
        // 原有对象列表
        List<Setmeal> setmealList = page.getRecords();
        // 现有对象集合
        List<SetmealDto> dtoList = setmealList.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            // 拷贝基本信息
            BeanUtils.copyProperties(item,setmealDto);
            // 获取分类ID
            Long categoryId = setmealDto.getCategoryId();
            // 查询分类对象
            Category category = categoryService.getById(categoryId);
            if (!ObjectUtils.isEmpty(category)){
                // 不为空设置分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        // 赋值给最终分页结果
        dtoPage.setRecords(dtoList);
        // 返回结果
        return dtoPage;
    }

    @Override
    public SetmealDto getOneById(Long id) {
        // 获取套餐基本信息
        Setmeal setmeal = this.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        // 复制
        BeanUtils.copyProperties(setmeal,setmealDto);
        // 获取菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        // 封装查询条件
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        // 查询
        List<SetmealDish> dishList = setmealDishService.list(queryWrapper);
       /* dishList = dishList.stream().map((item) -> {
            // 获取菜品ID
            Long dishId = item.getDishId();
            // 封装查询条件
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
            // 查询口味
            List<DishFlavor> flavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            DishDto dishDto = new DishDto();
            dishDto.setFlavors(flavorList);
            //return dishDto;
        }).collect(Collectors.toList());*/
        // 赋值
        setmealDto.setSetmealDishes(dishList);
        // 查询分类名称
        Long categoryId = setmealDto.getCategoryId();
        Category category = categoryService.getById(categoryId);
        // 非空则赋值
        if (!ObjectUtils.isEmpty(category)){
            String categoryName = category.getName();
            setmealDto.setCategoryName(categoryName);
        }
        // 返回套餐信息
        return setmealDto;
    }

    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        // 更新基本信息
        this.updateById(setmealDto);
        // 更新菜品信息
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        Long setmealId = setmealDto.getId();
        setmealDishList = setmealDishList.stream().map((item) -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        // 删除原有关系
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        // 构造条件
        queryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        // 删除
        setmealDishService.remove(queryWrapper);
        // 重新插入
        setmealDishService.saveBatch(setmealDishList);
    }

    @Override
    @Transactional
    public void updateWithCode(Integer code, Long[] ids) {
        List<Setmeal> setmealList = new ArrayList<>();
        Setmeal setmeal = null;
        for (Long id : ids) {
            setmeal = new Setmeal();
            setmeal.setId(id);
            setmeal.setStatus(code);
            setmealList.add(setmeal);
        }
        // 更新
        this.updateBatchById(setmealList);
    }

    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        // 保存基本信息
        this.save(setmealDto);
        // 获取ID
        Long setmealId = setmealDto.getId();
        // 获取菜品
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList = setmealDishList.stream().map((item) -> {
            item.setSetmealId(setmealId);
            return item;
        }).collect(Collectors.toList());
        // 保存菜品信息
        setmealDishService.saveBatch(setmealDishList);
    }

    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        // 查询状态
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        // 构造条件
        queryWrapper.in(Setmeal::getId,ids);
        // 启售
        queryWrapper.eq(Setmeal::getStatus,1);
        // 查询
        int count = this.count(queryWrapper);
        if (count > 0 ){
            // 不能删除
            throw new CustomException("套餐正在售卖中，不能删除");
        }
        // 可以删除
        this.removeByIds(ids);
        // 删除中间表
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 构造条件
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        // 删除
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }

    @Override
    public List<Setmeal> listBySetmeal(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        return this.list(queryWrapper);
    }

    @Override
    public List<DishDto> dishList(Long setmealId) {
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 构造查询条件
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealId);
        // 查询
        List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
        List<DishDto> dishDtoList = setmealDishList.stream().map((item) -> {
            Long dishId = item.getDishId();
            LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
            // 构造查询条件
            dishLambdaQueryWrapper.eq(Dish::getId,dishId);
            // 查询
            Dish dish = dishService.getOne(dishLambdaQueryWrapper);
            DishDto dishDto = new DishDto();
            // 赋值
            BeanUtils.copyProperties(dish,dishDto);
            // 获取菜品ID
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            // 构造查询条件
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
            // 查询
            List<DishFlavor> dishFlavorList = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
            // 赋值
            dishDto.setFlavors(dishFlavorList);
            // 返回
            return dishDto;
        }).collect(Collectors.toList());
        return dishDtoList;
    }
}
